﻿<%@ WebHandler Language="C#" Class="ZSEngineWorktimeReport" %>

using System;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Collections;
using System.IO;
using ReportBusiness;

/**
 * Description: 发动机工时统计报表
 * Copyright (c): 通力凯顿（北京）系统集成有限公司
 * Author: zhunan
 * Date: 2023.3.27
 */

public class ZSEngineWorktimeReport : IHttpHandler
{
    uMESReportOracleDAL dal = new uMESReportOracleDAL();
    public bool IsReusable { get { return false; } }
    public void ProcessRequest(HttpContext context)
    {
        string requestjson = "";
        using (StreamReader reader = new StreamReader(context.Request.InputStream)) { requestjson = reader.ReadToEnd(); }
        JObject jObject = JsonConvert.DeserializeObject(requestjson) as JObject;
        if (jObject["type"] != null)
        {
            try
            {
                Result r = new Result();
                context.Response.ContentType = "text/json";
                string type = jObject["type"].ToString();
                switch (type)
                {
                    case "getData":
                        r = getData(jObject);//查询
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e)
            {
                Result r = new Result();
                r.data = null;
                r.meta = new Result.Meta(500, e.Message);
                r.total = 0;
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }
        }
    }

    //查询公用sql
    string getQuerySql(JObject jObject)
    {
        JArray arr = JArray.Parse(jObject["checkdate"].ToString());
        string querysql = @"
SELECT wt.machineno,wt.checktype,wt.bigworkflowstepname,t.teamname,
       CASE WHEN wt.stepinfoid IS NOT NULL AND wt.checktype ='1'
            THEN COALESCE(wts.splitduration,wt.totalduration,ROUND(si.durationperunit / NVL(wt.splitcount, 1), 2), 0) 
            WHEN wt.stepinfoid IS NOT NULL AND wt.checktype ='2'
            THEN COALESCE(wts.splitduration,wt.totalduration, ROUND(si.durationperunit2 / NVL(wt.splitcount, 1), 2), 0)  
            ELSE COALESCE(wt.totalduration, wt.durationperunit, wts.splitduration, ROUND(sd.durationperunit / NVL(wt.splitcount, 1), 2), 0) 
       END worktime
FROM containersteptimesheet wt
LEFT JOIN containersteptimesheetsplit wts ON wts.containersteptimesheetid = wt.containersteptimesheetid
LEFT JOIN team t ON t.teamid = wt.teamid
LEFT JOIN stepschedulingdetail sd ON sd.stepid = wt.workflowstepid
LEFT JOIN stepinfo SI ON si.stepinfoid = wt.stepinfoid
WHERE wt.checkdate >= to_date('{0}','yyyy/mm/dd') AND wt.checkdate <= to_date('{1}','yyyy/mm/dd')";
        if (jObject["engineno"] != null && jObject["engineno"].ToString() != "")
        {
            querysql += string.Format(" AND wt.machineno = '{0}'", jObject["engineno"].ToString());
        }
        if (jObject["factoryid"] != null && jObject["factoryid"].ToString() != "")
        {
            querysql += string.Format(" AND wt.factoryid = '{0}'", jObject["factoryid"].ToString());
        }
        querysql = string.Format(querysql, arr[0].ToString(), arr[1].ToString(), jObject[""]);
        return querysql;
    }

    Result getData(JObject jObject)
    {
        string querysql = getQuerySql(jObject);
        //查询主要信息
        string sql = @"
SELECT t.machineno,ROUND(SUM(t.worktime)/60,2) worktime,
       ROUND(SUM(CASE WHEN t.checktype = 1 
                AND t.bigworkflowstepname <> '0050-工厂试车' 
                AND t.bigworkflowstepname <> '0120-检验试车' 
                AND t.teamname <> '封包组' THEN t.worktime ELSE 0 END)/60,2) czworktimetotal,
       ROUND(SUM(CASE WHEN t.checktype = 2 
                AND t.bigworkflowstepname <> '0050-工厂试车' 
                AND t.bigworkflowstepname <> '0120-检验试车' 
                AND t.teamname <> '封包组' THEN t.worktime ELSE 0 END)/60,2) jzworktimetotal,
       ROUND(SUM(CASE WHEN t.bigworkflowstepname = '0050-工厂试车' THEN t.worktime ELSE 0 END)/60,2) gcscworktimetotal,
       ROUND(SUM(CASE WHEN t.bigworkflowstepname = '0120-检验试车' THEN t.worktime ELSE 0 END)/60,2) jyscworktimetotal,
       ROUND(SUM(CASE WHEN t.teamname = '封包组' THEN t.worktime ELSE 0 END)/60,2) fbztotal
FROM
(
   {0}
) t
GROUP BY t.machineno";
        sql = string.Format(sql, querysql);
        DataTable dtResult = dal.GetDataTable(sql);

        dtResult.Columns.Add("czteamworktime", typeof(Object));//初装各组工时
        dtResult.Columns.Add("jzteamworktime", typeof(Object));//检装各组工时
        dtResult.Columns.Add("gcscteamworktime", typeof(Object));//工厂试车各组工时
        dtResult.Columns.Add("jyscteamworktime", typeof(Object));//检验试车各组工时

        //查初装各组工时
        sql = @"
SELECT t.machineno,t.teamname,ROUND(SUM(t.worktime)/60,2) worktime
FROM
(
   {0} AND wt.checktype = '1' AND wt.bigworkflowstepname NOT IN ( '0050-工厂试车','0120-检验试车') AND t.teamname <> '封包组'
) t
GROUP BY t.machineno,t.teamname";
        DataTable dtCZTeamWorkTime = dal.GetDataTable(string.Format(sql, querysql));

        //查检装各组工时
        sql = @"
SELECT t.machineno,t.teamname,ROUND(SUM(t.worktime)/60,2) worktime
FROM
(
   {0} AND wt.checktype = '2' AND wt.bigworkflowstepname NOT IN ( '0050-工厂试车','0120-检验试车') AND t.teamname <> '封包组'
) t
GROUP BY t.machineno,t.teamname";
        DataTable dtJZTeamWorkTime = dal.GetDataTable(string.Format(sql, querysql));

        //查工厂试车各组工时
        sql = @"
SELECT t.machineno,t.teamname,ROUND(SUM(t.worktime)/60,2) worktime
FROM
(
   {0} AND wt.bigworkflowstepname = '0050-工厂试车'
) t
GROUP BY t.machineno,t.teamname";
        DataTable dtGCSCTeamWorkTime = dal.GetDataTable(string.Format(sql, querysql));

        //查检验试车各组工时
        sql = @"
SELECT t.machineno,t.teamname,ROUND(SUM(t.worktime)/60,2) worktime
FROM
(
   {0} AND wt.bigworkflowstepname = '0120-检验试车'
) t
GROUP BY t.machineno,t.teamname";
        DataTable dtJYSCTeamWorkTime = dal.GetDataTable(string.Format(sql, querysql));

        foreach (DataRow dr in dtResult.Rows)
        {
            string filter = string.Format("machineno = '{0}'", dr["machineno"]);
            //绑定初装各组工时
            DataRow[] drCZList = dtCZTeamWorkTime.Select(filter);
            if (drCZList.Length > 0)
            {
                DataTable dtTeam = drCZList.CopyToDataTable();
                foreach (DataColumn col in dtTeam.Columns)
                {
                    col.ColumnName = col.ColumnName.ToLower();
                }
                dr["czteamworktime"] = dtTeam;
            }
            else
            {
                dr["czteamworktime"] = new DataTable();
            }

            //绑定检装各组工时
            DataRow[] drJZList = dtJZTeamWorkTime.Select(filter);
            if (drJZList.Length > 0)
            {
                DataTable dtTeam = drJZList.CopyToDataTable();
                foreach (DataColumn col in dtTeam.Columns)
                {
                    col.ColumnName = col.ColumnName.ToLower();
                }
                dr["jzteamworktime"] = dtTeam;
            }
            else
            {
                dr["jzteamworktime"] = new DataTable();
            }

            //绑定工厂试车各组工时
            DataRow[] drGCSCList = dtGCSCTeamWorkTime.Select(filter);
            if (drGCSCList.Length > 0)
            {
                DataTable dtTeam = drGCSCList.CopyToDataTable();
                foreach (DataColumn col in dtTeam.Columns)
                {
                    col.ColumnName = col.ColumnName.ToLower();
                }
                dr["gcscteamworktime"] = dtTeam;
            }
            else
            {
                dr["gcscteamworktime"] = new DataTable();
            }

            //绑定检验试车各组工时
            DataRow[] drJYSCList = dtJYSCTeamWorkTime.Select(filter);
            if (drJYSCList.Length > 0)
            {
                DataTable dtTeam = drJYSCList.CopyToDataTable();
                foreach (DataColumn col in dtTeam.Columns)
                {
                    col.ColumnName = col.ColumnName.ToLower();
                }
                dr["jyscteamworktime"] = dtTeam;
            }
            else
            {
                dr["jyscteamworktime"] = new DataTable();
            }
        }

        foreach (DataColumn col in dtResult.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        Result r = new Result();
        r.data = dtResult;
        r.total = Convert.ToInt32(dtResult.Rows.Count);
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }
}